package com.imd.s9.impl;

import com.alibaba.fastjson.JSONObject;
import com.imd.base.BusinessException;
import com.imd.s9.entity.TableInfo;
import com.imd.s9.service.StorageService;
import com.imd.s9.util.InitCriteria;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class StorageImpl implements StorageService {

    @Resource
    InitCriteria initCriteria;

    @Resource
    MongoTemplate mongoTemplate;

    @Override
    public void inOutRoom(String objCode, JSONObject doc) throws BusinessException {
        Boolean isExec = doc.getBoolean("单据状态");
        if(isExec == null)isExec = false;
        if(isExec){
            throw new BusinessException("该订单以执行");
        }
        if(ObjectUtils.isEmpty(doc.get("明细"))){
            throw new BusinessException("工单明细内容不能为空");
        }
        List<Map> list = doc.getObject("明细",List.class);
        List<Map> materials = new ArrayList<>();

        switch (objCode){
            case "66b48d00ac1bb2657a2c92ec":
                /**销售出库*/
                handler(objCode,doc,list,materials,false);
                break;
            case "66b48e19ac1bb2657a2c92f9":
                /**销售退料*/
                handler(objCode,doc,list,materials, true);
                break;
            case "66b9ae83f13c9b26892a3a07":
                /**生产领料退料*/
                handler(objCode,doc,list,materials,null);
                break;
            case "66b9b121f13c9b26892a3a0d":
                /**委外出库*/
                handler(objCode,doc,list,materials,false);
                break;
//            case "66c6f89df13c9b26892a3be5":
//                /**质检合格入库*/
//             handler(objCode,doc,list,materials,true);
//                break;
//            case "66c6f89df13c9b26892a3be5":
//                /**入库*/
//                handler(objCode,doc,list,materials,true);
//                break;
//            case "66c6f89df13c9b26892a3be5":
//                /**质检合格入库*/
//                handler(objCode,doc,list,materials,true);
//                break;
            default:
                break;
        }
    }

    public void handler(String objCode,JSONObject doc,List<Map> list,List<Map> materials,Boolean isOut) throws BusinessException{
        isOut = isOut==null?doc.getBoolean("单据类型"):isOut;
        String tenantId = doc.getString("tenantId");
        if(doc.get("明细")!=null&&!doc.getObject("明细",List.class).isEmpty()){
            for(int i =0;i<list.size();i++){
                Map temp = list.get(i);
                if(ObjectUtils.isEmpty(temp.get("物料编码"))){
                    throw new BusinessException("第"+(i+1)+"条物料编号不能为空");}
                String materialNo = ((Map)temp.get("物料编码")).getOrDefault("物料编码","").toString();
                Query query = initCriteria.create(tenantId,Criteria.where("物料编码").is(materialNo));
                Document material = mongoTemplate.findOne(query,Document.class,TableInfo.materialMana);
                if(material==null){ throw new BusinessException(materialNo+"物料编码不存在");}
                BigDecimal bg = new BigDecimal(0);
                Object number1 = ObjectUtils.isEmpty(temp.get("数量"))?0:temp.get("数量");
                Object number2 = ObjectUtils.isEmpty(material.get("数量"))?0:material.get("数量");
                if(new BigDecimal(number1.toString()).compareTo(BigDecimal.ZERO)<0){
                    throw new BusinessException(materialNo+"物料数量不能小于0");
                }
                if(isOut){
                    bg = new BigDecimal(number2.toString()).
                            add(new BigDecimal(number1.toString()));
                }else {
                    bg = new BigDecimal(number2.toString()).
                            subtract(new BigDecimal(number1.toString()));
                    if(bg.compareTo(BigDecimal.ZERO)<0){
                        throw new BusinessException(materialNo+"领用物料数量不足");
                    }
                }
                material.put("数量",bg);
                materials.add(material);
            }
            for(Map material:materials){
                String materialNo = material.getOrDefault("物料编码","").toString();
                Query query = initCriteria.create(tenantId,Criteria.where("物料编码").is(materialNo));
                Update update = new Update();
                update.set("数量",material.get("数量"));
                mongoTemplate.updateFirst(query,update,TableInfo.materialMana);
            }
            Query query = initCriteria.create(tenantId,Criteria.where("单据编号").is(doc.get("单据编号")));
            Update update = new Update();
            update.set("订单状态",true);
            update.set("执行时间",new Date());
            mongoTemplate.updateFirst(query,update,"entity_"+objCode);
        }
    }

}
